package com.demo2do.fms.admin.web.controller.data.cost;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.utils.CollectionUtils;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Preloading;
import com.demo2do.fms.admin.entity.search.data.RoadBridgeFeeSearch;
import com.demo2do.fms.admin.entity.security.Login;
import com.demo2do.fms.admin.service.data.RoadBridgeFeeService;
import com.demo2do.fms.admin.web.resolver.Secure;
import com.demo2do.fms.entity.authority.Organization;
import com.demo2do.fms.entity.data.cost.RoadBridgeFee;
import com.demo2do.fms.entity.data.cost.Ton;

/**
 * 路桥费管理
 * 
 * @author Wy
 *
 */
@Controller
@RequestMapping("/road-bridge-fees")
@MenuSetting("menu-data")
public class RoadBridgeFeeController {
    
    @Autowired
    @Qualifier("roadBridgeFeeService")
    private RoadBridgeFeeService roadBridgeFeeService;
    
    /**
     * list
     * 
     * @param orgIds
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-road-bridge-fee")
    public ModelAndView list(@Secure(property = "orgIds") List<Long> orgIds, Page page){
        
        ModelAndView modelAndView = new ModelAndView("data/cost/road-bridge-fee/road-bridge-fee-list");
        
        Organization organization = roadBridgeFeeService.selectOrganization(orgIds, page);
        
        if(organization != null) {
        
            List<RoadBridgeFee> roadBridgeFees = roadBridgeFeeService.list(organization);
        
            modelAndView.addObject("page", page);
            modelAndView.addObject("roadBridgeFees", CollectionUtils.constructMapValues(roadBridgeFees, "ton"));
            modelAndView.addObject("organization", organization);

            modelAndView.addObject("tons", Ton.values());
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * display search panel
     * 
     * @param orgIds
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    @MenuSetting("submenu-road-bridge-fee")
    public ModelAndView search(@Secure(property = "orgIds") List<Long> orgIds){
        
        ModelAndView modelAndView = new ModelAndView("data/cost/road-bridge-fee/road-bridge-fee-search");
        
        modelAndView.addObject("orgIds", orgIds);
        
        return modelAndView;
    }
    
    /**
     * search-list,不支持分页
     * 
     * @param roadBridgeFeeSearch
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET ,params= "filter")
    @MenuSetting("submenu-road-bridge-fee")
    public ModelAndView onSearch(RoadBridgeFeeSearch roadBridgeFeeSearch){
        
        ModelAndView modelAndView = new ModelAndView("data/cost/road-bridge-fee/road-bridge-fee-list");
        
        List<RoadBridgeFee> roadBridgeFees = roadBridgeFeeService.list(roadBridgeFeeSearch);
            
        if(org.apache.commons.collections.CollectionUtils.isNotEmpty(roadBridgeFees)) {
            modelAndView.addObject("roadBridgeFees", CollectionUtils.constructMapValues(roadBridgeFees, "ton"));
            modelAndView.addObject("organization", roadBridgeFees.get(0).getOrganization());
        }
        
        modelAndView.addObject("tons", Ton.filter(roadBridgeFeeSearch.getMinTon(), roadBridgeFeeSearch.getMaxTon()));
        
        return modelAndView;
    }
    
    /**
     * display create panel
     * 
     * @param orgId
     * @param ton
     * @param emp
     * @return
     */
    @MenuSetting("submenu-road-bridge-fee")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public ModelAndView create(@RequestParam("organization") Long orgId, 
                               @RequestParam("ton") Ton ton,
                               @RequestParam("empty") boolean emp){
        
        ModelAndView modelAndView = new ModelAndView("data/cost/road-bridge-fee/road-bridge-fee-create");
        
        modelAndView.addObject("orgId", orgId);
        modelAndView.addObject("ton", ton);
        modelAndView.addObject("emp", emp);
        
        return modelAndView;
    }
    
    /**
     * on create
     * 
     * @param roadBridgeFee
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onCreate(RoadBridgeFee roadBridgeFee) {
        
        HttpResult httpResult = new HttpResult();
        
        roadBridgeFeeService.create(roadBridgeFee);
        
        httpResult.success().message("新增成功");
        
        return httpResult;
    }

    /**
     * display edit panel
     * 
     * @param roadBridgeFeeId
     * @return
     */
    @MenuSetting("submenu-road-bridge-fee")
    @RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
    public ModelAndView edit(@PathVariable("id") Long roadBridgeFeeId){
        
        ModelAndView modelAndView = new ModelAndView("data/cost/road-bridge-fee/road-bridge-fee-edit");
        
        RoadBridgeFee roadBridgeFee = roadBridgeFeeService.load(RoadBridgeFee.class, roadBridgeFeeId);
        
        modelAndView.addObject("roadBridgeFee", roadBridgeFee);
        
        return modelAndView;
    }
    
    /**
     * on edit
     * 
     * @param login
     * @param roadBridgeFee
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onEdit(@Secure Login login, @Preloading("roadBridgeFeeId") RoadBridgeFee roadBridgeFee){
        
        HttpResult httpResult = new HttpResult();
        
        roadBridgeFeeService.edit(login.getId(),roadBridgeFee);
        
        httpResult.success().message("设置成功");
        
        return httpResult;
    }
    
}